package com.example.myfirestdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

public class CheckBoxActivity extends AppCompatActivity {

    private CheckBox mCb1, mCb2, mCb3, mCb4, mCb5, mCb6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_box);

        mCb1 = findViewById(R.id.cb_1);
        mCb2 = findViewById(R.id.cb_2);
        mCb3 = findViewById(R.id.cb_3);
        mCb4 = findViewById(R.id.cb_4);
        mCb5 = findViewById(R.id.cb_5);
        mCb6 = findViewById(R.id.cb_6);

        mCb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(CheckBoxActivity.this, isChecked ? "选中了" : "未选中", Toast.LENGTH_SHORT).show();
            }
        });

        CheckBoxBtnClick(mCb1);
        CheckBoxBtnClick(mCb2);
        CheckBoxBtnClick(mCb3);
        CheckBoxBtnClick(mCb4);
        CheckBoxBtnClick(mCb5);
        CheckBoxBtnClick(mCb6);
    }

    void CheckBoxBtnClick(CheckBox cb) {
        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                String cbText = buttonView.getText().toString();

                Toast.makeText(CheckBoxActivity.this, isChecked ? cbText + "选中了" : cbText + "未选中", Toast.LENGTH_SHORT).show();
            }
        });
    }
}